package org.jks.excel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.jks.report.model.ReportDeviceRe;
import org.jks.util.DateUtil;

public class DeviceReUtil {
	public static List<String> columns = new ArrayList<String>();
	static {
		columns.add("序号");
		columns.add("飞行日期");
		columns.add("飞行类型");
		columns.add("架次");
		columns.add("里程（Km）");
		columns.add("时长（min）");
		columns.add("起降点信息");
		columns.add("系统异常信息");
	}
	
	public static void export(HttpServletResponse response, List<ReportDeviceRe> res) {
		response.setContentType("applicationnd.ms-excel"); 
		
		try{
			String filename = java.net.URLEncoder.encode("无人机履历表", "UTF-8")+ "-"+DateUtil.toString(new Date(), "yyyyMMddHHmmss") + ".xls";
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + filename); 
			ServletOutputStream sos = response.getOutputStream();
			WritableWorkbook workbook = Workbook.createWorkbook(sos);
			WritableSheet sheet = workbook.createSheet("sheet1", 0);
			int colCount = columns.size();
			for(int i=0; i<colCount; i++){
				sheet.setColumnView(i, 30);
			}
			sheet.mergeCells(0, 0, colCount-1, 0);
			WritableFont wf = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
			WritableCellFormat wcf = new WritableCellFormat(wf);
			wcf.setAlignment(Alignment.CENTRE);
//			wcf.setBackground(Colour.GRAY_50);
			Label lab = null;
			lab = new Label(0, 0, "国网四川省电力公司检修公司无人机飞行履历表", wcf);
			sheet.addCell(lab);
			
			
			WritableFont wf1 = new WritableFont(WritableFont.TIMES, 14, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
			WritableCellFormat wcf1 = new WritableCellFormat(wf1);
			
			sheet.mergeCells(0, 1, colCount-1, 0);
			sheet.mergeCells(0, 2, colCount-1, 0);
			sheet.mergeCells(0, 3, colCount-1, 0);
			
			lab = new Label(0, 1, "单位：", wcf1);
			sheet.addCell(lab);
			
			lab = new Label(0, 2, "无人机名称及编号：" + res.get(0).getDeviceName() + res.get(0).getDeviceNo(), wcf1);
			sheet.addCell(lab);
			
			lab = new Label(0, 3, "验收投运日期：", wcf1);
			sheet.addCell(lab);
			
//			wcf1.setBackground(Colour.GRAY_25);
			for(int i=0; i<colCount; i++){
				lab = new Label(i, 4, columns.get(i), wcf1);
				sheet.addCell(lab);
			}
			
			int index = 5;			
			for(int rowIndex = 0; rowIndex<res.size(); rowIndex++)
			{
				ReportDeviceRe re = res.get(rowIndex);
				
				lab = new Label(0, index, (rowIndex+1) + "");
				sheet.addCell(lab);
				lab = new Label(1, index, re.getFlightTime());
				sheet.addCell(lab);
				lab = new Label(2, index, re.getType());
				sheet.addCell(lab);
				lab = new Label(3, index, re.getCount());
				sheet.addCell(lab);
				lab = new Label(4, index, re.getMileage());
				sheet.addCell(lab);
				lab = new Label(5, index, re.getDuration());
				sheet.addCell(lab);
				lab = new Label(6, index, re.getInfo());
				sheet.addCell(lab);
				lab = new Label(7, index, re.getExceptionInfo());
				sheet.addCell(lab);
				
				index++;
			}
			workbook.write();
			workbook.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}